//
//  ZYLoseSearchController.m
//  生活小助手
//
//  Created by 振亚 姜 on 14-8-7.
//  Copyright (c) 2014年 lxz. All rights reserved.
//

#import "ZYLoseSearchController.h"
#import "MBProgressHUD+MJ.h"
#import "ZYIdentityLoseParam.h"
#import "ZYIdentityLoseResult.h"
#import "ZYIdentityLose.h"
#import "XZFunctionKey.h"
#import "ZYIdentityTool.h"

@interface ZYLoseSearchController ()
@property (nonatomic,strong) UITextField *identityNoField;
@property (nonatomic,strong) UILabel *resultLabel;
@property (nonatomic,assign) BOOL netOK;
@end

@implementation ZYLoseSearchController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"greenTableBG"]];
    self.view.y = 93;
    self.view.height = kScreenHeight - self.view.y;
    [self addViews];
}

-(void)addViews
{
    /* ------------------身份证号码输入框------------------*/
    
    UILabel *label = [[UILabel alloc] init];
    label.font = kIdentityBigFont;
    label.text = @"身份证号";
    CGSize size = [label.text sizeWithAttributes:@{NSFontAttributeName: label.font}];
    label.x = kMargin;
    label.y = kMargin;
    label.width = size.width;
    label.height = 30;
    [self.view addSubview:label];
    
    NSString *placeholder = @"请输入18或15位身份证号码";
    UITextField *identityNoField = [UITextField textFieldWithPlaceholder:placeholder leftMargin:5];
    identityNoField.font = kIdentityNormalFont;
    CGSize numSize = [placeholder sizeWithAttributes:@{NSFontAttributeName: identityNoField.font}];
    identityNoField.x = CGRectGetMaxX(label.frame) + kMargin;
    identityNoField.y = label.y;
    identityNoField.width = numSize.width + kMargin;
    identityNoField.height = label.height;
    self.identityNoField = identityNoField;
    [self.view addSubview:identityNoField];
    /*--------------查询按钮------------*/
    UIButton *queryBtn = [[UIButton alloc] init];
    [queryBtn setTitle:@"查询" forState:UIControlStateNormal];
    [queryBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    queryBtn.backgroundColor = [UIColor orangeColor];
    queryBtn.width = 60;
    queryBtn.height = 30;
    queryBtn.x = (kScreenWidth - queryBtn.width)* 0.5;
    queryBtn.y = CGRectGetMaxY(identityNoField.frame) + kMargin;
    [queryBtn addTarget:self action:@selector(getIdentityInfo) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:queryBtn];
    
    /*---------------结果显示---------------*/
    UILabel *resultLabel = [[UILabel alloc] init];
    resultLabel.font = kIdentityBigFont;
    resultLabel.x = kMargin;
    resultLabel.y = CGRectGetMaxY(queryBtn.frame) + kMargin * 2;
    resultLabel.width = kScreenWidth - 2 * kMargin;
    resultLabel.height = kIdentityBigFont.lineHeight * 2;
    resultLabel.numberOfLines = 0;
    [self.view addSubview:resultLabel];
    self.resultLabel = resultLabel;
}

-(void)getIdentityInfo
{
    ZYIdentityLoseParam *param = [[ZYIdentityLoseParam alloc] init];
    param.key = [XZFunctionKey identityKey];
    param.dtype = @"json";
    param.cardno = [self.identityNoField.text lowercaseString];
    
    [MBProgressHUD showMessage:@"正在帮您查询"];
    
    [ZYIdentityTool getIdentityLoseWithParameter:param success:^(ZYIdentityLoseResult *responseObj) {
        self.netOK = YES;
        ZYIdentityLose *info = responseObj.result;
        self.resultLabel.text = [NSString stringWithFormat:@"身份证号码: %@\n挂失状态: %@",self.identityNoField.text,info.tips];
        LLog(@"身份证号码: %@\n说  明: %@",self.identityNoField.text,info.tips);
        [MBProgressHUD hideHUD];
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showError:error.domain];
    }];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (!self.netOK) {
            [MBProgressHUD hideHUD];
            [MBProgressHUD showError:@"请检查你的网络"];
        }
        
    });
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}


@end
